<?php

    // breadcrumbs
    $this->breadcrumbs=array(
        'Sensors'=>array('index'),
        'Manage',
    );

?>

<div class="page-header">
    <?php echo CHtml::link('<i class="icon-plus icon-white"></i> Create sensor', '#', array(
        'onclick'=>"{ addSensor(); $('#modalCreate').modal('show');}",
        'class'=>'btn btn-primary',
    )) ?>
    <h1>Manage Sensors</h1>
</div>

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'sensor-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'name',
        'description',
        array(
            'name'=>'create_time',
            'type'=>'datetime',
            'filter'=>false,
        ),
        array(
            'name'=>'update_time',
            'type'=>'datetime',
            'filter'=>false,
        ),
        array(
            'class'=>'CButtonColumn',
            'template'=>'{view} {update} {delete}',
            'viewButtonImageUrl'=>Yii::app()->baseUrl.'/css/admin/gridview/view.png',
            // Tooltip for view button
            'viewButtonOptions'=>array(
                'rel'=>'tooltip',
                'data-original-title'=>'Details',
                'title'=>'',
            ),
            // Tooltip for update button
            'updateButtonOptions'=>array(
                'rel'=>'tooltip',
                'data-original-title'=>'Edit',
                'title'=>'',
            ),
            'deleteButtonImageUrl'=>Yii::app()->baseUrl.'/css/admin/gridview/delete.png',
            // Tooltip for delete button
            'deleteButtonOptions'=>array(
                'rel'=>'tooltip',
                'data-original-title'=>'Delete',
                'title'=>'',
            ),
            'buttons'=>array(
                'update'=>array(
                    'imageUrl'=>Yii::app()->baseUrl.'/css/admin/gridview/update.png',
                    'click'=>"function(e){
                        e.preventDefault();
                        $('#modalUpdate .modal-body').children(':eq(0)').empty(); // Stop auto POST
                        $('#modalUpdate .modal-header h3').html('Update Sensor');
                        updateModal($(this).attr('href'));
                        $('#modalUpdate').modal('show');
                    }",
                ),
                'delete'=>array(
                    'click'=>"function( e ){
                        e.preventDefault();
                        $('#modalUpdate .modal-body').children(':eq(0)').empty(); // Stop auto POST
                        $('#modalUpdate .modal-header h3').html('Delete Confirm');
                        updateModal($(this).attr('href'));
                        $('#modalUpdate').modal('show');
                    }",
                ),
            ),
        ),
    ),
)); ?>

<!-- Modal for Creating Sensor -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id'=>'modalCreate',
    'options'=>array(
        'title'=>'Create Sensor',
        // 'width'=>600,
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<script type="text/javascript">
// Add a sensor by ajax
function addSensor()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('sensor/create'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            if (data.status == 'failure')
            {
                $('#modalCreate div.modal-body').html(data.content);
                // Here is the trick: on submit-> once again this function!
                $('#modalCreate div.modal-body form').submit(addSensor);
            }
            else
            {
                //console.log(data);
                $('#modalCreate div.modal-body').html(data.content);
                setTimeout(\"$('#modalCreate').modal('hide') \",2000);
                setTimeout(\"$.fn.yiiGridView.update('sensor-grid', { data: $(this).serialize() }) \",2000);
            }

        } ",
    )); ?>
    return false; 
}
</script>

<!-- Modal for Update -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id'=>'modalUpdate',
    'options'=>array(
        'title'=>'Update Sensor',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<?php
$updateJS = CHtml::ajax(array(
    'url'=>"js:url",
    'data'=>"js:form.serialize() + action",
    'type'=>'post',
    'dataType'=>'json',
    'success'=>"function(data)
    {
        if(data.status == 'failure')
        {
            $('#modalUpdate div.modal-body').html(data.content);
            $('#modalUpdate div.modal-body form input[type=submit]')
                .die() // Stop from re-binding event handlers
                .live('click', function(e){ // Send clicked button value
                e.preventDefault();
                updateModal(false, $(this).attr('name'));
            });
        }
        else
        {
            $('#modalUpdate div.modal-body').html(data.content);
            if(data.status == 'success') // Update all grid views on success
            {
                $('div.grid-view').each(function(){ // Change the selector if you use different class or element
                    $.fn.yiiGridView.update($(this).attr('id'));
                });
            }
            $('#modalUpdate').modal('hide');
            $('#modalUpdate .modal-body').children(':eq(0)').empty();
        }
    }"
)); ?>

<?php
Yii::app()->clientScript->registerScript('updateModal', "function updateModal(url, act)
    {
        var action = '';
        var form = $('#modalUpdate div.modal-body form');
        if(url == false)
        {
            action = '&action=' + act;
            url = form.attr('action');
        }
        {$updateJS}
    }" 
); ?>